package com.airlines.runlimit.controller;

import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.runlimit.domain.SchLimit;
import com.airlines.runlimit.service.SchLimitService;
import com.airlines.system.domain.SysUser;

/**
 * 
 * @ClassName: SchLimitController
 * @Description: 人员限制
 * @author jiaxw
 * @date 2018年9月18日
 *
 */
@Controller
@RequestMapping("/schlimit")
public class SchLimitController {

	private static final String PREFIX = "/runlimit";

	@Autowired
	private SchLimitService schLimitService;

	/**
	 * 
	 * @Title: toSchLimitPage
	 * @Description: 进入页面
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @return
	 *
	 */
	@RequestMapping(value = "", method = { RequestMethod.GET })
	public String toSchLimitPage() {
		return PREFIX + "/schLimitList";
	}

	/**
	 * 
	 * @Title: list
	 * @Description: 多条件模糊 查询并且分页显示
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param schLimit
	 * @param offset
	 * @param limit
	 * @return
	 *
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Page list(SchLimit schLimit, String offset, String limit) {
		Page page = new Page();
		page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
		page.setPageSize(Integer.valueOf(limit));
		schLimit.setIsDelete(0);
		page = schLimitService.listData(schLimit, page);
		return page;
	}

	/**
	 * 
	 * @Title: add
	 * @Description: 进入新增页面
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @return
	 *
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.GET })
	public String add() {
		return PREFIX + "/schLimitAdd";
	}

	/**
	 * 
	 * @Title: save
	 * @Description: 保存
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param schLimit
	 * @param request
	 * @return
	 *
	 */
	@ResponseBody
	@RequestMapping(value = "/save", method = { RequestMethod.POST })
	public R save(SchLimit schLimit, HttpServletRequest request) {
		SysUser user = (SysUser) request.getSession().getAttribute("currentUser");
		schLimit.setId(UUID.randomUUID().toString());
		schLimit.setCreateUser(user.getWorkNo());
		schLimit.setIsDelete(0);
		schLimit.setCreateTime(new Date());
		if (schLimitService.save(schLimit) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 
	 * @Title: edit
	 * @Description: 进入编辑页面
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param model
	 * @param id
	 * @return
	 *
	 */
	@RequestMapping(value = "/edit", method = { RequestMethod.GET })
	public String edit(Model model, String id) {
		SchLimit schLimit = schLimitService.getByPrimaryKey(id);
		model.addAttribute("schLimit", schLimit);
		return PREFIX + "/schLimitEdit";
	}

	/**
	 * 
	 * @Title: update
	 * @Description: 修改
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param schLimit
	 * @param request
	 * @return
	 *
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public R update(SchLimit schLimit, HttpServletRequest request) {
		SysUser user = (SysUser) request.getSession().getAttribute("currentUser");
		schLimit.setUpdateUser(user.getWorkNo());
		schLimit.setUpdateTime(new Date());
		if (schLimitService.updateByPrimaryKeySelective(schLimit) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 
	 * @Title: view
	 * @Description: 进入查看页面
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param model
	 * @param id
	 * @return
	 *
	 */
	@RequestMapping(value = "/view", method = { RequestMethod.GET })
	public String view(Model model, String id) {
		SchLimit schLimit = schLimitService.getByPrimaryKey(id);
		model.addAttribute("schLimit", schLimit);
		model.addAttribute("isReadonly", 1);
		return PREFIX + "/schLimitEdit";
	}

	/**
	 * 
	 * @Title: delete
	 * @Description: 删除
	 * @author jiaxw
	 * @date 2018年9月18日
	 * @param id
	 * @return
	 *
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = { RequestMethod.POST })
	public R delete(String id) {
		if (schLimitService.deleteByPrimaryKey(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

}
